package com.jiangyao.controller;


import com.jiangyao.common.ApiResult;
import com.jiangyao.common.constants.CacheConstants;
import com.jiangyao.component.RedisComponent;
import com.jiangyao.model.dto.backend.AdminUserDTO;
import com.jiangyao.model.dto.backend.AdminUserListPageDTO;
import com.jiangyao.model.dto.backend.AdminUserSearchDTO;
import com.jiangyao.model.vo.AdminUserSessionVO;
import com.jiangyao.model.vo.backend.AdminUserBaseVO;
import com.jiangyao.model.vo.backend.AdminUserListVO;
import com.jiangyao.service.AdminUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 管理员信息 前端控制器
 * </p>
 *
 * @author yangyongchuan
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/adminUser")
@Slf4j
@Api(tags = "系统管理-后台管理用户")
public class AdminUserController extends BaseController {

	@Autowired
	private AdminUserService adminUserService;

	@Autowired
	private RedisComponent redisComponent;

	/**
	 * 登录
	 *
	 * @param adminUserDTO 登录DTO
	 * @return 结果
	 */
	@ApiOperation(value = "后台管理用户登录", notes = "必填字段：loginName，password")
	@PostMapping("/login")
	public ApiResult<AdminUserSessionVO> login(@Validated({AdminUserDTO.Login.class}) @RequestBody AdminUserDTO adminUserDTO) {
		// 登录,返回用户登录信息，与凭证
		return new ApiResult<>(adminUserService.login(adminUserDTO));
	}


	/**
	 * 获取登录用户信息
	 *
	 * @return 返回登录用户信息
	 */
	@ApiOperation(value = "获取登录用户信息")
	@PostMapping("/loginInfo")
	public ApiResult<AdminUserSessionVO> loginInfo() {
		return new ApiResult<>(getLoginSession());
	}

	/**
	 * 退出登录
	 *
	 * @return 结果
	 */
	@ApiOperation(value = "退出登录")
	@PostMapping("/logout")
	public ApiResult<String> logout() {
		redisComponent.del(CacheConstants.CACHE_KEY_TOKEN_BACKEND_PREFIX + getLoginToken());
		return new ApiResult<>();
	}


	/**
	 * 获取账号列表
	 *
	 * @param adminUserListDTO 列表DTO
	 * @return 结果
	 */
	@ApiOperation(value = "获取账号列表")
	@PostMapping("/list")
	public ApiResult<AdminUserListVO> getListPage(@Validated({AdminUserListPageDTO.ListPage.class}) @RequestBody AdminUserListPageDTO adminUserListDTO) {
		return ApiResult.success(adminUserService.getListPage(adminUserListDTO), AdminUserListVO.class);
	}

	/**
	 * 增加账户
	 *
	 * @param adminUserDTO 增加DTO
	 * @return 结果
	 */
	@ApiOperation(value = "增加账号", notes = "字段：password不填，默认为123456")
	@PostMapping("/add")
	public ApiResult<String> add(@Validated({AdminUserDTO.Add.class}) @RequestBody AdminUserDTO adminUserDTO) {
		adminUserService.add(adminUserDTO);
		return new ApiResult<>("ok");
	}

	/**
	 * 编辑账户
	 *
	 * @param adminUserDTO 编辑账户DTO
	 * @return 结果
	 */
	@ApiOperation(value = "编辑账户", notes = "字段：账号不能修改；password不填，不修改；")
	@PostMapping("/edit")
	public ApiResult<String> edit(@Validated({AdminUserDTO.Edit.class}) @RequestBody AdminUserDTO adminUserDTO) {
		adminUserService.edit(adminUserDTO);
		return new ApiResult<>("ok");
	}

	/**
	 * 编辑我的账户
	 *
	 * @param adminUserDTO 编辑账户DTO
	 * @return 结果
	 */
	@ApiOperation(value = "编辑我的账户", notes = "字段：账号不能修改；password不填，不修改；")
	@PostMapping("/editMyself")
	public ApiResult<String> editMyself(@Validated({AdminUserDTO.Edit.class}) @RequestBody AdminUserDTO adminUserDTO) {
		adminUserService.editMyself(adminUserDTO);
		return new ApiResult<>("ok");
	}

	/**
	 * 修改(重置)密码
	 *
	 * @param adminUserDTO 修改密码DTO
	 * @return 结果
	 */
	@ApiOperation(value = "修改(重置)密码", notes = "字段：adminId必填，password不填，默认为123456；其他字段不传值")
	@PostMapping("/editPassword")
	public ApiResult<String> editPassword(@RequestBody AdminUserDTO adminUserDTO) {
		adminUserService.editPassword(adminUserDTO);
		return new ApiResult<>("ok");
	}

	/**
	 * 根据姓名检索账号（最多返回50）,用于组织、班级选择管理员
	 *
	 * @param adminUserSearchDTO 检索账号DTO
	 * @return 结果
	 */
	@ApiOperation(value = "根据姓名检索账号（最多返回50）,用于组织、班级选择管理员")
	@PostMapping("/searchForSelect")
	public ApiResult<AdminUserBaseVO> searchForSelect(@RequestBody AdminUserSearchDTO adminUserSearchDTO) {
		return ApiResult.success(adminUserService.searchForSelect(adminUserSearchDTO), AdminUserBaseVO.class);
	}

}

